package com.aizuda.boot.modules.business.department.controller;

import com.aizuda.boot.modules.system.entity.vo.department.*;
import com.aizuda.boot.modules.system.service.ISysDepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 部门管理控制器 - 兼容原department模块的接口
 * 
 * @author system
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/department")
@Slf4j
@Validated
@Tag(name = "部门管理", description = "部门管理相关接口")
public class DepartmentController {

    @Resource
    private ISysDepartmentService sysDepartmentService;

    @Operation(summary = "全部部门集合")
    @GetMapping("/getAllDepartmentNames")
    public Map<Long, String> getAllDepartmentNames() {
        return sysDepartmentService.getAllDepartmentNames();
    }

    @Operation(summary = "获取完整的部门组织架构树")
    @GetMapping("/tree")
    public List<DepartmentTreeVO> getDepartmentTree() {
        return sysDepartmentService.getDepartmentTreeForCompatibility();
    }

    @Operation(summary = "根据父节点ID获取部门组织架构树")
    @GetMapping("/tree/{parentNodeId}")
    public List<DepartmentTreeVO> getDepartmentTreeByParentId(
            @Parameter(description = "父节点ID", required = true)
            @PathVariable Long parentNodeId) {
        return sysDepartmentService.getDepartmentTreeByParentIdForCompatibility(parentNodeId);
    }

    @Operation(summary = "获取部门详细信息，包括子部门和员工信息")
    @PostMapping("/detail")
    public DepartmentDetailVO getDepartmentDetail(
            @RequestBody DepartmentDetailRequest request) {
        return sysDepartmentService.getDepartmentDetailForCompatibility(request.getId());
    }

    @GetMapping("/main/{employeeId}")
    @Operation(summary = "根据员工ID获取主部门（优先）或任一所属部门")
    public DepartmentInfoDTO getMainDepartment(
            @Parameter(description = "员工ID") @PathVariable Long employeeId) {
        return sysDepartmentService.getEmployeeMainDepartmentForCompatibility(employeeId);
    }

    @Operation(summary = "根据租户获取部门组织树")
    @GetMapping("/tree/tenant/{tenantId}")
    public DepartmentTreeVO getDepartmentTreeByTenant(@PathVariable Integer tenantId) {
        return sysDepartmentService.getDepartmentTreeByTenantForCompatibility(tenantId.longValue());
    }

    @Operation(summary = "根据系统租户获取部门组织树")
    @GetMapping("/tenantTree/{sysTenantKey}")
    public DepartmentTreeVO getDepartmentTreeBySysTenantKey(@PathVariable String sysTenantKey){
        return sysDepartmentService.getDepartmentTreeBySysTenantKey(sysTenantKey);
    }

    @Operation(summary = "根据用户权限获取部门组织树")
    @GetMapping("/tree/user")
    public List<DepartmentTreeVO> getDepartmentTreeByUser() {
        return sysDepartmentService.getDepartmentTreeByUser();
    }

    @Operation(summary = "获取租户顶级部门信息")
    @GetMapping("/tenantDepartment")
    public List<DepartmentTenantDTO> getTenantDepartmentInfoList(){
        return sysDepartmentService.getTenantDepartmentInfoList();
    }
} 